<template>
  <div class="home-page">
    <!-- 首页横幅 -->
    <div class="banner">
      <div class="banner-content">
        <h1>欢迎来到校园活动管理系统</h1>
        <p>发现精彩校园活动，丰富大学生活</p>
        <router-link to="/activity-list" class="banner-btn">
          浏览活动
        </router-link>
      </div>
    </div>
    
    <!-- 热门活动 -->
    <section class="section">
      <div class="section-header">
        <h2>热门活动</h2>
        <router-link to="/activity-list?status=hot" class="more-link">更多</router-link>
      </div>
      <div class="activity-grid">
        <ActivityCard v-for="activity in hotActivities" :key="activity.id" :activity="activity" />
        <div v-if="hotActivities.length === 0" class="empty-state">
          <p>暂无热门活动</p>
        </div>
      </div>
    </section>
    
    <!-- 即将开始的活动 -->
    <section class="section">
      <div class="section-header">
        <h2>即将开始</h2>
        <router-link to="/activity-list?status=upcoming" class="more-link">更多</router-link>
      </div>
      <div class="activity-grid">
        <ActivityCard v-for="activity in upcomingActivities" :key="activity.id" :activity="activity" />
        <div v-if="upcomingActivities.length === 0" class="empty-state">
          <p>暂无即将开始的活动</p>
        </div>
      </div>
    </section>
    
    <!-- 活动分类 -->
    <section class="section categories-section">
      <div class="section-header">
        <h2>活动分类</h2>
      </div>
      <div class="categories-grid">
        <CategoryCard v-for="category in categories" :key="category.id" :category="category" />
      </div>
    </section>
    
    <!-- 快速入口 -->
    <section class="quick-access">
      <div class="access-item">
        <div class="access-icon">
          <el-icon><calendar /></el-icon>
        </div>
        <h3>发布活动</h3>
        <p>轻松创建和管理你的活动</p>
        <router-link to="/activity-edit" class="access-btn">立即发布</router-link>
      </div>
      <div class="access-item">
        <div class="access-icon">
          <el-icon><ticket /></el-icon>
        </div>
        <h3>活动报名</h3>
        <p>一键报名感兴趣的活动</p>
        <router-link to="/activity-list" class="access-btn">去报名</router-link>
      </div>
      <div class="access-item">
        <div class="access-icon">
          <el-icon><user /></el-icon>
        </div>
        <h3>个人中心</h3>
        <p>管理你的活动和报名</p>
        <router-link to="/profile" class="access-btn">我的账户</router-link>
      </div>
    </section>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import ActivityCard from '../components/ActivityCard.vue'
import CategoryCard from '../components/CategoryCard.vue'
import { Calendar, Ticket, User } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getHotActivities, getUpcomingActivities } from '../api/activity'
import { getAllCategories } from '../api/category'

export default {
  name: 'HomePage',
  components: {
    ActivityCard,
    CategoryCard,
    Calendar,
    Ticket,
    User
  },
  setup() {
    // 使用ref存储数据
    const hotActivities = ref([])
    const upcomingActivities = ref([])
    const categories = ref([])
    
    // 加载首页数据
    const loadHomeData = async () => {
      try {
        // 直接调用API获取数据
        const [hotResponse, upcomingResponse, categoriesResponse] = await Promise.all([
          getHotActivities(3),
          getUpcomingActivities(3),
          getAllCategories()
        ])
        
        hotActivities.value = (hotResponse.data || []).slice(0, 6)
        upcomingActivities.value = (upcomingResponse.data || []).slice(0, 6)
        categories.value = (categoriesResponse || []).slice(0, 6)
      } catch (error) {
        ElMessage.error('加载首页数据失败')
      }
    }
    
    onMounted(() => {
      loadHomeData()
    })
    
    return {
      hotActivities,
      upcomingActivities,
      categories
    }
  }
}
</script>

<style scoped>
.home-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 横幅样式 */
.banner {
  height: 400px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  margin-bottom: 40px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.banner::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,%3Csvg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"%3E%3Cpath d="M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z" fill="%23ffffff" fill-opacity="0.1" fill-rule="evenodd"/%3E%3C/svg%3E');
}

.banner-content h1 {
  font-size: 48px;
  margin-bottom: 16px;
  position: relative;
  z-index: 1;
}

.banner-content p {
  font-size: 20px;
  margin-bottom: 30px;
  position: relative;
  z-index: 1;
}

.banner-btn {
  background: white;
  color: #667eea;
  padding: 12px 36px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s;
  position: relative;
  z-index: 1;
  display: inline-block;
}

.banner-btn:hover {
  background: #f0f0f0;
  transform: translateY(-2px);
}

/* 区块样式 */
.section {
  margin-bottom: 60px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.section-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0;
}

.more-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
}

.more-link:hover {
  color: #66b1ff;
}

/* 活动网格 */
.activity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

/* 分类网格 */
.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}

/* 快速入口 */
.quick-access {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 60px;
}

.access-item {
  background: white;
  border-radius: 8px;
  padding: 30px;
  text-align: center;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
}

.access-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.access-icon {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 16px;
}

.access-item h3 {
  font-size: 18px;
  color: #303133;
  margin-bottom: 8px;
}

.access-item p {
  color: #606266;
  margin-bottom: 20px;
}

.access-btn {
  display: inline-block;
  background: #409eff;
  color: white;
  padding: 8px 24px;
  border-radius: 20px;
  text-decoration: none;
  font-size: 14px;
  transition: background 0.3s;
}

.access-btn:hover {
  background: #66b1ff;
  color: white;
}

/* 空状态 */
.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: #909399;
  background: #f5f7fa;
  border-radius: 8px;
}

/* 响应式 */
@media (max-width: 768px) {
  .banner {
    height: 300px;
  }
  
  .banner-content h1 {
    font-size: 32px;
  }
  
  .banner-content p {
    font-size: 16px;
  }
  
  .activity-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
  
  .categories-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }
  
  .quick-access {
    grid-template-columns: 1fr;
  }
}
</style>